package com.exrobot.module.rbt.service.robotbox;

import cn.hutool.core.collection.CollUtil;
import com.exrobot.framework.common.util.collection.CollectionUtils;
import com.exrobot.framework.common.util.date.LocalDateTimeUtils;
import com.exrobot.module.rbt.controller.admin.orderitem.vo.OrderItemPageReqVO;
import com.exrobot.module.rbt.dal.dataobject.orderitem.OrderItemDO;
import com.exrobot.module.rbt.dal.dataobject.robotinfo.RobotInfoDO;
import com.exrobot.module.rbt.dal.redis.RbtRedisDAO;
import com.exrobot.module.rbt.enums.RbtOrderStatusEnum;
import com.exrobot.module.rbt.service.order.OrderService;
import com.exrobot.module.rbt.service.orderitem.OrderItemService;
import jodd.util.StringUtil;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.exrobot.module.rbt.controller.admin.robotbox.vo.*;
import com.exrobot.module.rbt.dal.dataobject.robotbox.RobotBoxDO;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.util.object.BeanUtils;

import com.exrobot.module.rbt.dal.mysql.robotbox.RobotBoxMapper;

import static com.exrobot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.exrobot.framework.common.util.collection.CollectionUtils.convertList;
import static com.exrobot.framework.common.util.collection.CollectionUtils.diffList;
import static com.exrobot.module.rbt.enums.ErrorCodeConstants.*;

/**
 * 餐车和盒配置 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RobotBoxServiceImpl implements RobotBoxService {

    @Resource
    private RobotBoxMapper robotBoxMapper;

    @Resource
    private OrderItemService orderItemService;

    @Resource
    private RbtRedisDAO rbtRedisDAO;

    @Override
    public Long createRobotBox(RobotBoxSaveReqVO createReqVO) {
        // 插入
        RobotBoxDO robotBox = BeanUtils.toBean(createReqVO, RobotBoxDO.class);
        robotBoxMapper.insert(robotBox);

        // 返回
        return robotBox.getId();
    }

    @Override
    public void updateRobotBox(RobotBoxSaveReqVO updateReqVO) {
        // 校验存在
        validateRobotBoxExists(updateReqVO.getId());
        // 更新
        RobotBoxDO updateObj = BeanUtils.toBean(updateReqVO, RobotBoxDO.class);
        robotBoxMapper.updateById(updateObj);
    }

    @Override
    public void deleteRobotBox(Long id) {
        // 校验存在
        validateRobotBoxExists(id);
        // 删除
        robotBoxMapper.deleteById(id);
    }

    @Override
    public void deleteRobotBoxListByIds(List<Long> ids) {
        // 删除
        robotBoxMapper.deleteByIds(ids);
        }


    private void validateRobotBoxExists(Long id) {
        if (robotBoxMapper.selectById(id) == null) {
            throw exception(RBT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public RobotBoxDO getRobotBox(Long id) {
        return robotBoxMapper.selectById(id);
    }

    @Override
    public PageResult<RobotBoxDO> getRobotBoxPage(RobotBoxPageReqVO pageReqVO) {
        return robotBoxMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RobotBoxDO> getRobotBoxByRobotSn(String robotSn) {
        if(StringUtil.isBlank(robotSn)) {
            robotSn = "0";
        }
        List<RobotBoxDO> list = robotBoxMapper.selectList(RobotBoxDO::getRobotSn,robotSn);
        if(list == null || list.isEmpty()) {
            return robotBoxMapper.selectList(RobotBoxDO::getRobotSn,"0");
        }
        return null;
    }

    @Override
    public List<RobotBoxDO> getNoUseRobotBoxByRobotSn(String robotSn) {
        List<RobotBoxDO> list = getRobotBoxByRobotSn(robotSn);
        if (list == null || list.isEmpty()) {
            return list;
        }
        List<Integer> boxSns = rbtRedisDAO.getLockErr(robotSn);
        Map<Integer, OrderItemDO> orderItemDOList = orderItemService.getOrderItemMap(robotSn);
        if (orderItemDOList != null && !orderItemDOList.isEmpty()) {
            boxSns.addAll(orderItemDOList.keySet());
        }
        if (boxSns.isEmpty()) {
            return list;
        } else {
            return list.stream().filter(w -> !boxSns.contains(w.getBoxNo())).toList();
        }
    }

    @Override
    public Map<String,List<RobotBoxDO>> getRobotBoxMap(Collection<String> robotSns) {
        robotSns.add("0");
        RobotBoxPageReqVO reqVO = new RobotBoxPageReqVO();
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        reqVO.setRobotSns(robotSns);
        List<RobotBoxDO> list = getRobotBoxPage(reqVO).getList();
        if (list == null || list.isEmpty()) {
            return new HashMap<>();
        }
        return CollectionUtils.convertMultiMap(list,RobotBoxDO::getRobotSn);
    }


}